package animation;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;

import main.GameCanvas;
import model.Position;

import utilities.CRes;
import utilities.FrameImage;
import utilities.GameResource;

//falling star animation
public class FallingStar extends Effect{
	public int m_iTimer = 50;
	public int m_iNumParticle = 5;
	private FrameImage[] m_arrFrameImg;
	
	public void startEffect(int x, int y){
		super.startEffect(x, y);
		m_arrPoint.removeAllElements();
		for(int i = 0; i < m_iNumParticle; i++){
			Position pos = new Position(CRes.random(10) + x, y);
			pos.anchor = 4 - CRes.random(3);
			m_arrPoint.addElement(pos);
		}
		
		m_arrFrameImg = new FrameImage[4];
		for(int i = 0; i < 4; i++){
			m_arrFrameImg[i] = new FrameImage(GameResource.instance.imgEffect[i], 7, 7);
		}
	}
			
	public void startEffect(int x, int y, int colorIndex, int widthEffect){
		super.startEffect(x, y);
		m_arrPoint.removeAllElements();
		for(int i = 0; i < m_iNumParticle; i++){
			Position pos = new Position(CRes.random(widthEffect) + x, y);
			pos.anchor = 4 - CRes.random(3);
			m_arrPoint.addElement(pos);
		}
		
		m_arrFrameImg = new FrameImage[1];
		m_arrFrameImg[0] = new FrameImage(GameResource.instance.imgEffect[colorIndex], 7, 7);
	}
	
	
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		int a = m_arrPoint.size();
		for(int i = 0; i < a; i++){
			Position pos = (Position)m_arrPoint.elementAt(i);
			m_arrFrameImg[i % m_arrFrameImg.length].drawSeriFrame(pos.x, pos.y, Sprite.TRANS_NONE, g);
		}
	}

	public void update() {
		// TODO Auto-generated method stub
		int a = m_arrPoint.size();
		for(int i = 0; i < a; i++){
			Position pos = (Position)m_arrPoint.elementAt(i);
			pos.y += pos.anchor;
			if(pos.y > m_posCenterEffect.y + 50){
				m_arrPoint.removeElementAt(i);
				Position pos1 = new Position(CRes.random(10) + m_posCenterEffect.x, m_posCenterEffect.y);
				pos1.anchor = 4 - CRes.random(3);
				m_arrPoint.insertElementAt(pos1, i);
			}
		}
		
		m_iGameTick++;
		if(m_iGameTick > m_iTimer){
			CRes.cleanVector(m_arrPoint);
			
			int b = m_arrFrameImg.length;
			for(int i = 0; i < b; i++){
				m_arrFrameImg[i] = null;
			}
			m_arrFrameImg = null;
			
			//remove effect
			GameCanvas.m_arrEffect.removeElement(this);
		}
	}
	
}